<?php
namespace Controllers;
use Resources, Models, Libraries;

class Msmhs extends Resources\Controller
{    
	public function __construct(){
		parent::__construct();
		$this->session = new Resources\Session;
		$this->rest = new Resources\Rest;
		$this->request = new Resources\Request;
		$this->table = new Models\Msmhs;
		$this->DT = new Libraries\Datatables;
		$this->validation   = new Models\FormValidationMsmhs;
	}
	
	public function index(){
		$data['title'] = 'Data Mahasiswa';
		$data['halaman']='Msmhs';
		$data['form'] = false;
		$this->output('default/template',$data);
	}	
	
	public function init($action='read'){    
        if (IS_AJAX) {
			$action = $this->request->post('action');
			switch ($action){
				default:
					$this->_read();
				break;
				case 'read':
					$this->_read();
				break;
				case 'create':
					$this->_create($this->request->post('data'));
				break;
				case 'edit':
					$this->_update($this->request->post('id'));
				break;
				case 'remove':
					$this->_delete($this->request->post('data'));
				break;
			}
		}
    }
	
	public function add(){
		$data['title'] = 'Create Mahasiswa'; 
		$data['halaman'] = 'msmhs';
		$data['form'] = true;
		$data['action']='create';
		$data['style'] = "";
		
		if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
            if( $this->validation->validate() ){
                $data ['validate']['valPost']= $this->validation->value();
				$data ['validate']['lastInsert'] = $this->table->Create($this->validation->value());
				$data ['style'] = "has-success";
			}else{
				$data ['style'] = "has-error";
			}
        }
		$data['validasi'] = $this->validation;
		$this->output('default/template',$data);
	}
	
	public function edit($id=0){
		$data['title'] = 'Update Mahasiswa'; 
		$data['halaman'] = 'msmhs';
		$data['form'] = true;
		$data['action']='edit';
		$data['value'] = Resources\Tools::objectToArray($this->table->getOne(array("IDMSMHS"=>$id)));
		$data['style'] = "";
		
		if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
			$this->validation->checkNIDN = false;
			
            if( $this->validation->validate() ){
                $data ['validate']['valPost']= $this->validation->value();
				$data ['validate']['lastUpdate'] = $this->table->Update($this->validation->value(), array("IDMSMHS"=>$id));
				$data ['style'] = "has-success";
			}else{
				$data ['style'] = "has-error";
			}
        }
		$data['validasi'] = $this->validation;

		$this->output('default/template',$data);
	}	
	public function _read(){
		$this->DT
		->primary("IDMSMHS")
		->select(array('NIMHSMSMHS','NMMHSMSMHS','TPLHRMSMHS','TGLHRMSMHS','KDJEKMSMHS','TAHUNMSMHS'))
		->from('msmhs');
		echo $this->DT->generate();
	}
	
	private function _update($id){
		$data = $this->rest->getRequest();
		$data['requestMethod'] = $this->rest->requestMethod;
		$doupdate= $this->request->post('data');
		$dowhere = array('IDMSMHS'=> $this->request->post('id',FILTER_SANITIZE_NUMBER_INT));
		$this->table->update($doupdate,$dowhere);
		$this->outputJSON($data, 200);
	}
	
	private function _delete($dowhere){
		$data = $this->rest->getRequest();
		$data['requestMethod'] = $this->rest->requestMethod;
		$this->table->delete($dowhere);
		$this->outputJSON($data, 200);
	}
	
	private function _create($fields){
		$data = $this->rest->getRequest();
		$data['requestMethod'] = $this->rest->requestMethod;
		$this->table->create($fields);
		$this->outputJSON($data, 200);
	}
	
} 